package com.kaifamiao.types;

public class Recursion {
	
	public long fibo( long n ) {
		// 如果不考虑 n <= 0 的情况，当参数为 零 或 负整数 时会抛出 StackOverflowError
		if( n == 1 || n == 2 ) {
			return 1 ;
		}
		return fibo( n - 1 ) + fibo( n - 2 );
	}
	
	public long factorial( long n ) {
		if( n < 0 ) {
			throw new RuntimeException( "你丫数学是体育老师教的？" );
		}
		if( n == 0 || n == 1 ) {
			return 1 ;
		}
		return n * factorial( n - 1 );
	}

	public static void main(String[] args) {
		Recursion r = new Recursion();
		//long result = r.fibo( -1 ); // StackOverflowError
		long result = r.fibo(3);
		System.out.println( result );
		
		System.out.println( r.factorial( 5 ) );
	}

}
